/**
 * https://leetcode.cn/problems/find-smallest-letter-greater-than-target/
 */
function nextGreatestLetter(letters: string[], target: string): string {
  let start = 0,
    end = letters.length;
  if (target >= letters[end - 1]) {
    return letters[start];
  }

  while (start < end) {
    const mid = (start + end) >> 1;
    if (letters[mid] > target) {
      // 往前找
      end = mid;
    } else {
      start = mid + 1;
    }
  }

  return letters[start];
}

function nextGreatestLetterV1(letters: string[], target: string): string {
  for (let letter of letters) {
    if (letter.charCodeAt(0) - target.charCodeAt(0) > 0) {
      return letter;
    }
  }
  return letters[0];
}
